/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package dao;

import pojo.Dimensionmediadata;
import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Roaa
 */
public class DimMediaDataCrud {

    Connection con;
    PreparedStatement stmt;

    public DimMediaDataCrud() throws SQLException {
        try {
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dimensionproject", "root", "root");
        } catch (InstantiationException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }

    }

    public boolean update(Dimensionmediadata mData) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("UPDATE dimensionmediadata SET length=?, length_by=?, width=?, width_by=?, depth=?, depth_by=?, weight=?, weight_by=?, pages=?, pages_by=?, format=?, format_by=?, mail_format=?, mail_format_by=?, items=?, items_by=?, rec_update_date=? WHERE upc= ?");
            stmt.setString(18, mData.getUpc());
            stmt.setInt(1, mData.getLength());
            stmt.setShort(2, mData.getLengthBy());
            stmt.setInt(3, mData.getWidth());
            stmt.setShort(4, mData.getWidthBy());
            stmt.setInt(5, mData.getDepth());
            stmt.setShort(6, mData.getDepthBy());
            stmt.setInt(7, mData.getWeight());
            stmt.setShort(8, mData.getWeightBy());
            stmt.setInt(9, mData.getPages());
            stmt.setShort(10, mData.getPagesBy());
            stmt.setString(11, mData.getFormat());
            stmt.setShort(12, mData.getFormatBy());
            stmt.setString(13, mData.getMailFormat());
            stmt.setShort(14, mData.getMailFormatBy());
            stmt.setInt(15, mData.getItems());
            stmt.setShort(16, mData.getItemsBy());
            System.out.println(mData.getLengthBy());
            stmt.setDate(17, new Date(mData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    public boolean insert(Dimensionmediadata mData) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("INSERT INTO dimensionmediadata VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
            stmt.setString(1, mData.getUpc());
            stmt.setInt(2, mData.getLength());
            stmt.setShort(3, mData.getLengthBy());
            stmt.setInt(4, mData.getWidth());
            stmt.setShort(5, mData.getWidthBy());
            stmt.setInt(6, mData.getDepth());
            stmt.setShort(7, mData.getDepthBy());
            stmt.setInt(8, mData.getWeight());
            stmt.setShort(9, mData.getWeightBy());
            stmt.setInt(10, mData.getPages());
            stmt.setShort(11, mData.getPagesBy());
            stmt.setString(12, mData.getFormat());
            stmt.setShort(13, mData.getFormatBy());
            stmt.setString(14, mData.getMailFormat());
            stmt.setShort(15, mData.getMailFormatBy());
            stmt.setInt(16, mData.getItems());
            stmt.setShort(17, mData.getItemsBy());
            System.out.println(mData.getLengthBy());
            stmt.setDate(18, new Date(mData.getRecUpdateDate().getTime()));
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }

    public boolean delete(String upc) {
        int f = 0;
        boolean result = false;
        try {
            stmt = con.prepareStatement("DELETE FROM dimensionmediadata WHERE upc LIKE ?");
            stmt.setString(1, upc);
            f = stmt.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(DimBookDataCrud.class.getName()).log(Level.SEVERE, null, ex);
        }
        result = (f==1)?true:false;
        return result;
    }
}
